﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.DAL.DBExtend;
using System.Data.SqlClient;
using ZX.SYS.Library;
using ZX.SYS.Bussiness.DeskTop;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ZX.Web.UI.Controls;


namespace ZX.WebApp.DeskTop.SetDesktop
{
    public partial class SetDesktopItem : ListPage
    {
        private string action = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            action = Request["Action"];
            switch (action)
            {
                case "GetDeskTopTemplateList": GetDeskTopTemplateList(); break;
                case "GetSysRoleDesktopList": GetSysRoleDesktopList(); break;
                case "AddRoleDeskTop": AddRoleDeskTop(); break;
                case "Delete": DeleteTempData(); break;
                case "CheckData": LoadData(); break;
            }
        }
        /// <summary>
        /// 桌面模板表
        /// </summary>
        private void GetDeskTopTemplateList()
        {
            string RoleID = Request["RoleID"];
            var json = string.Empty;
            List<SysDesktopTemplate> list = BDeskTop.GetDeskTopTemplateList(RoleID);
            if (list != null)
                json = JsonConvert.SerializeObject(list);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(json);
            Response.End();
        }
        /// <summary>
        /// 角色桌面表
        /// </summary>
        private void GetSysRoleDesktopList()
        {
            string RoleID = Request["RoleID"];
            var json = string.Empty;
            List<SysRoleDesktop> list = BDeskTop.GetSysRoleDesktopList(RoleID);
            if (list != null)
                json = JsonConvert.SerializeObject(list);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(json);
            Response.End();
        }
        /// <summary>
        /// 添加角色桌面
        /// </summary>
        private void AddRoleDeskTop()
        {
            string getData = Request["PostData"];
            List<SysRoleDesktop> list = new List<SysRoleDesktop>();
            var obj = JsonConvert.DeserializeObject<List<SysRoleDesktopHelp>>(getData);
            if (obj.Count > 0)
            {
                for (int i = 0; i < obj.Count; i++)
                {
                    string tsrc = string.Empty;
                    string height = string.Empty;
                    SysRoleDesktop srt = new SysRoleDesktop();
                    srt = BDeskTop.GetRoleDesktopEntity(obj[i].DeskTopTempID);
                    if (srt != null && srt.RoleDesktopID.ToString().Length > 0)
                    {
                        height = srt.Height;
                    }

                    SysRoleDesktop sdh = new SysRoleDesktop
                    {
                        DeskTopTempID = new Guid(obj[i].DeskTopTempID),
                        RoleId = new Guid(obj[i].RoleId),
                        Sort = obj[i].Sort,
                        Height = height
                    };
                    list.Add(sdh);
                }
            }
            else {
                SysRoleDesktop sdh = new SysRoleDesktop();
                sdh.RoleId = new Guid(Request["RoleID"]);
                list.Add(sdh);
            }
            string returnMsg = BDeskTop.ModifySysRoleDeskTop(list);
            returnMsg = JsonConvert.SerializeObject(returnMsg);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(returnMsg);
            Response.End();
        }
        /// <summary>
        /// 删除模板数据
        /// </summary>
        private void DeleteTempData()
        {
            string DeskTopTempID = Request["DeskTopTempID"];
            bool del = BDeskTop.DeleteDeskTopTemplate(DeskTopTempID);
            string msg = string.Empty;
            if (del) msg = "保存成功";
            else msg = "保存失败，您已经配置所要删除的桌面，操作失败，请在桌面设置中取消对桌面的设置，再执行该操作";
            msg = JsonConvert.SerializeObject(msg);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(msg);
            Response.End();
        }

        protected void LoadData()
        {
            string RoleDesktopID = Request["RoleDesktopID"];
            string RoleID = Request["RoleID"];
            SysRoleDesktop srt = new SysRoleDesktop();
            srt = BDeskTop.GetRoleDesktopEntity(RoleDesktopID);
            string msg = string.Empty;

            if (srt != null && srt.RoleDesktopID.ToString().Length > 0)
                msg = "true";
            else msg = "false";

            msg = JsonConvert.SerializeObject(msg);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(msg);
            Response.End();
        }
    }

    public class SysRoleDesktopHelp
    {
        public string DeskTopTempID { get; set; }
        public string RoleId { get; set; }
        public int Sort { get; set; }
    }
}